package com.example.demo.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 代理类，必须实现InvocationHandler方法
 */
public class UserProxy implements InvocationHandler {

    /** 用户自定义拦截-日志、权限 */
    private LogInterception logInterception;
    private PermissionInterception permissionInterception;
    /** 代理目标 */
    private Object target;

    public UserProxy(LogInterception logInterception, PermissionInterception permissionInterception, Object target) {
        this.logInterception = logInterception;
        this.permissionInterception = permissionInterception;
        this.target = target;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

        System.out.println("代理方法执行开始");

        this.logInterception.showLogger();

        if (permissionInterception.getUserPermission().equals("admin")) {
            permissionInterception.showPermission();
            method.invoke(this.target, objects);
        } else {
            System.out.println("用户没有操作权限");
        }

        System.out.println("代理方法执行结束");

        return null;
    }
}
